using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using AutoMapper;
using Core.SimpleTemp.Repositories.Internal;
using Microsoft.EntityFrameworkCore;
using Core.SimpleTemp.Api.Mobile;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// //法律法规_标准库
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class FL_StandardInformationController : MobileBaseConteroller
    {
        private readonly FL_StandardInformationService _service;
        private readonly CoreDBContext _coreDBContext;

        /// <summary>
        /// FL_StandardInformation
        ///</summary>
        public FL_StandardInformationController(FL_StandardInformationService service, CoreDBContext coreDBContext)
        {
            _service = service;
            _coreDBContext = coreDBContext;
        }


        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        [PermissionFilter(nameof(FL_StandardInformationController) + "_" + nameof(FL_StandardInformationController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<FL_StandardInformationDto>
                pagingQueryModel)
        {
            IPageModel<FL_StandardInformationDto> result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new { a.Id, a.LawRegulationsName }).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }



        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var entity = await _service.GetAsync(id);
            if (entity == null) return this.JsonFaild("未找到对应数据:" + id);

            //法律法规_标准库相关部门
            var standardInformationDeptDto = Mapper.Map<List<FL_StandardInformationDeptDto>>(entity.FL_StandardInformations);
            var standardInformationDept = standardInformationDeptDto?.Select(a =>
            new
            {
                departmentName = a.Mark01,//部门名称
                a.DeptType,
              //  a.DeptTypeText,//
                a.FitSituation,//符合情况
                a.FitSituationOrNotDescribe//不符合描述
            });

            //附件
            var attachmentList = await _coreDBContext.SysFile.Where(a => a.B_ID == id).Select(a => "/api/File/DownLoad?id=" + a.Id).ToListAsync();

            var retModel = new
            {
                entity.Id,//id
                entity.LawRegulationsName,//法律法规名称
                entity.IssueSubject,//发布主体
                entity.UpdateTime,//更改时间
                entity.ApplyClause,//适用条款
                entity.DescribeForWork,//对应工作情况描述
                entity.Symbol,//文号/标准号
                entity.IssueDate,//颁布日期
                entity.ImplementationDate,//实施日期
                standardInformationDept,
                gkbmList = standardInformationDept?.Where(a => a.DeptType == "1").Select(a => a.departmentName),//了解知道
                zxbmList = standardInformationDept?.Where(a => a.DeptType == "1" || a.DeptType == "2").Select(a => a.departmentName),//熟练掌握  //指导他人
                attachmentList,
            };

            return this.JsonSuccess(retModel);
        }

    }
}
